package org.example.tree;

/**
 * @author liyishan
 * @date 2024/8/5 10:41
 * @apiNote
 */

public class Test00563FindTilt {
    int res = 0;

    public int findTilt(TreeNode root) {
        dfs(root);
        return res;
    }

    public int dfs(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftSum = dfs(node.left);
        int rightSum = dfs(node.right);
        res += Math.abs(leftSum - rightSum);
        return leftSum + rightSum + node.val;
    }
}
